<?php

namespace App\Models\Traits;

use Illuminate\Database\Eloquent\Builder;
use Carbon\Carbon;

trait HasUserGoldCoin
{
	public function isGoldCoinsEnough($coins): bool
	{
		return big_decimal($coins)->isLessThanOrEqualTo($this->gold_coins);
	}

    public function hadSupportedGoldCoins(): bool
    {
        return $this->goldCoins()
            ->where(function (Builder $query) {
                $query->where('remark', 'support_gold_coins')
                    ->orWhere('remark', 'transfered_in_coins');
            })->exists();
    }

	public function transferedInCoins()
	{
		return $this->goldCoins()->TransferedIn()->sum('money');
	}

	public function transferedOutCoins()
	{
		return $this->goldCoins()->TransferedOut()->sum('money');
	}

	public function payOrderCoins()
	{
		return $this->goldCoins()->PayOrder()->sum('money');
	}

	public function netProfitTotal()
	{
		return $this->goldCoins()->NetProfit()->sum('money');
	}

	public function goldBeansTotal()
	{
		return $this->goldCoins()->ResoldBeans()->sum('money');
	}

	public function preNetProfitTotal($time)
	{
		return $this->goldCoins()->NetProfit()->where('created_at', '>', $time)->sum('money');
	}

	public function preGoldRecordTotal($time)
	{
		return $this->goldCoins()->PayGoldRecord()->where('created_at', '>', $time)->sum('money');
	}

	public function preGoldBeansTotal($time)
	{
		return $this->goldCoins()->ResoldBeans()->where('created_at', '>', $time)->sum('money');
	}

	public function goldRecordTotal()
	{
		return $this->goldCoins()->PayGoldRecord()->sum('money');
	}

	public  function yesterdayGoldCoins()
	{
		$money = $this->fromGoldCoins()->where('remark', 'gold_record_direct_prize')
					->whereDate("created_at",Carbon::today())->sum('money');

		return sprintf("%.2f",$money);
	}
}
